<?php
//////////////// DATABASE CONNECTION /////////
  $host = 'localhost';
  $username = 'root';
  $password = '';
  $db = 'b21';
  mysql_connect($host,$username, $password) or die(mysql_error());
  mysql_select_db($db) or die(mysql_error());
/////////////////////////////////////////

function pr($array=array()) {
  echo "<pre style=\"color:white;background:black;font-size:20px;\">";
  print_r($array);
  echo "</pre>";
}

function showQuery($array=array()) {
  // set it to true when to want to debug query
   $isShow = true;
   $isShow = false;
  
  if($isShow == true){
    echo "<pre style=\"color:white;background:black;font-size:20px;\">";
    print_r($array);
    echo "</pre>";
  }
}


function vx($array=array()) {
  echo "<pre style=\"color:white;background:black;font-size:20px;\">";
  var_export($array);
  echo "</pre>";
}


/***
 * USed to insert data in database
 * @param $table STRING Table Name
 * @param $array ARRAY column=>value pair
 * $return INT
 * */
function insert($table='',$array=array())
{

  if($table=='' || empty($array)) return false;

  $query = '';
  foreach($array as $column => $value) {
    $query .= "`$column`='$value',";
  }
  $query = rtrim($query,',');
  $query =  "INSERT INTO `$table` SET  ".$query;
   showQuery($query);
  mysql_query($query) or die(mysql_error());
  return mysql_insert_id();
}



function update($table='',$array=array(),$where="")
{

  if($table=='' || empty($array)|| empty($where)) 
  return false;

  $query = '';
  foreach($array as $column => $value) {
    $query .= "`$column`='$value',";
  }
  $query = rtrim($query,',');

  $query =  "UPDATE `$table` SET  ".$query." $where";

  showQuery($query);

  mysql_query($query) or die(mysql_error());
  return mysql_affected_rows();
}


function fetchById($table='',$id=0)
{
  $query =  "SELECT * FROM `$table` WHERE `id`=$id";
   showQuery($query);
  $result = mysql_query($query) or die(mysql_error());
  return $result;
}

function fetchAll($table='')
{
  $query =  "SELECT * FROM `$table` ";
   showQuery($query);
  $result = mysql_query($query) or die(mysql_error());
  return $result;
}

//
// function getStatus($status=0){
  // // if($status == 0) {
   // // return 'Inactive';
  // // } else {
    // // return 'Active';
  // // }
//
  // return ($status == 0) ? 'Inactive':'Active';
// }//getStatus


function getStatus($status=0){
   if($status == 0) {
    return 'Inactive';
   } else {
     return 'Active';
   }
}//getStatus


function getGender($gender=0){
  return ($gender == 0)?'Male':'Female';
}//getGender



function deleteById($table='',$id=0){
  $query =  "DELETE FROM `$table` WHERE `id` = '$id'";
   showQuery($query);
  mysql_query($query) or die(mysql_error());
  return mysql_affected_rows();
}

function deleteByCondition($table='',$where=""){
    
  if($where=="")return false;
  
  $query =  "DELETE FROM `$table` WHERE $where";
   showQuery($query);
  mysql_query($query) or die(mysql_error());
  return mysql_affected_rows();
}


function languages(){
	return array('c'=>'C','php'=>'PHP','chash'=>'C#','dotnet'=>'.Net','perl'=>'Perl');
		
}
